package com.smart.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smart.campus.dao.SysRoleMapper;
import com.smart.campus.dto.SysRole;
import com.smart.campus.pojo.PagedResponse;
import com.smart.campus.service.SysRoleService;
import com.smart.campus.util.PaginationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRole> findSysRoleList() {
        return sysRoleMapper.selectList(null);
    }

    @Override
    public PagedResponse<SysRole> pageRole(SysRole sysRole, Integer pageNum, Integer pageSize) {
        Page<SysRole> page = new Page<>(pageNum, pageSize);
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
//        角色名称
        if (sysRole != null && !"".equals(sysRole.getRoleName()) && sysRole.getRoleName() != null){
            queryWrapper.like("role_name",sysRole.getRoleName());
        }
        //角色状态
        if (sysRole != null && !"".equals(sysRole.getStatus()) && sysRole.getStatus() != null){
            queryWrapper.eq("status",sysRole.getStatus());
        }
        Page<SysRole> page1 = sysRoleMapper.selectPage(page,queryWrapper);
        return PaginationUtil.getPaginationResponse(page1.getRecords(),pageNum,pageSize,page1.getTotal());
    }

    @Override
    public void addRole(SysRole sysRole) {
        sysRoleMapper.insert(sysRole);
    }

    @Override
    public void delSysRole(Integer roleId) {
        sysRoleMapper.deleteById(roleId);
    }

    @Override
    public SysRole findRoleById(Integer roleId) {
        return sysRoleMapper.selectById(roleId);
    }

    @Override
    public void updateRole(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }

    @Override
    public void updateRoleStatus(SysRole sysRole) {
        sysRoleMapper.updateById(sysRole);
    }
}
